package com.promis.okhttp;

import com.promis.crypto.BASE64Coder;
import com.promis.util.CollectionUtils;
import com.promis.util.EncodingUtils;
import com.promis.util.JsonUtils;
import com.promis.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.IdentityHashMap;

/**
 * Created by jidanbo on 2018/2/27.
 */

public class HttpJsonParams {

    private final IdentityHashMap hashMap = new IdentityHashMap();

    public void add(String key, String value) {
        if (!StringUtils.isNullOrEmpty(value)) {
            hashMap.put(key, value);
        }
    }

    public void add(String key, String[] values) {
        if (!CollectionUtils.isNullOrEmpty(values)) {
            hashMap.put(key, values);
        }
    }

    public void add(String key, Number value) {
        if (value != null) {
            hashMap.put(key, value);
        }
    }

    public void add(String key, Number[] values) {
        if (!CollectionUtils.isNullOrEmpty(values)) {
            hashMap.put(key, values);
        }
    }

    public void add(String key, Boolean value) {
        if (value != null) {
            hashMap.put(key, value);
        }
    }

    public void add(String key, Boolean[] values) {
        if (!CollectionUtils.isNullOrEmpty(values)) {
            hashMap.put(key, values);
        }
    }

    public void add(String key, Date value) {
        if (value != null) {
            hashMap.put(key, value);
        }
    }

    public String getJsonParams() {
        String jsonParams = JsonUtils.toJson(hashMap);
        try {
            String base64Params = BASE64Coder.encryptBASE64(jsonParams.getBytes(EncodingUtils.ENCODING_UTF8));
            return EncodingUtils.ajaxEncode(base64Params);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }
    }
}
